package SocialMediaSentiment;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class calcTrigramValues {

	static HashMap<String, Double> comparePosNeg;
	static HashMap<String, Double> compareZeroNonZero;

	public static double calcLimits(HashMap<String, Integer> trigrams,
			double upper, double lower, Map<String, Double>  woordenweightslijst) throws IOException {

		HashMap<String, Double[]> tweetmap = new HashMap<String, Double[]>();
		double tweetValue;
		
		Iterator<String> k = (Iterator<String>) trigrams.keySet().iterator();

		while (k.hasNext()) {
			String trigrampertweet = k.next();
			int sentiment = trigrams.get(trigrampertweet);
			tweetValue = StandardFunctions.CalculateTweetValue(
					woordenweightslijst, trigrampertweet);
			Double[] values = { tweetValue, (double) sentiment };
			tweetmap.put(trigrampertweet, values);
		}
		Iterator<String> k2 = (Iterator<String>) tweetmap.keySet().iterator();
		int fouten = 0;
		int aantal = 0;

		while (k2.hasNext()) {
			String key = k2.next();
			// out.println(key);
			aantal++;
			// out.println(tweetmap.get(key)[0] + " " + tweetmap.get(key)[1] );
			if (tweetmap.get(key)[0] > upper) {
				if (tweetmap.get(key)[1] <= 0) {
					fouten++;
				}
			} else if (tweetmap.get(key)[0] < lower) {
				if (tweetmap.get(key)[1] >= 0) {
					fouten++;
				}
			} else if (tweetmap.get(key)[1] != 0) {
				fouten++;
			}

			// out.println("-------------------------------");

		}
		// System.out.println("Aantal tweets:  " + aantal);
		// System.out.println("Aantal fout: "+ fouten);
//		System.out.println("Percentage fout:  " + (double) (fouten)
//				/ (double) aantal * 100);
		// out.close();
		return (double) (fouten) / (double) aantal * 100;
	}

	@SuppressWarnings("unused")
	private static double calculateTrigramValue(String word1, String word2,
			String word3) throws IOException {
		// System.out.println( " WORD1 : "+ word1);
		// System.out.println( " WORD2 : "+ word2);
		// System.out.println( " WORD3 : "+ word3);
		// System.out.println( " ==============" );
		Double sentValue1;
		Double sentValue2;
		Double sentValue3;
		try {
			sentValue1 = (1 - compareZeroNonZero.get(word1)) / 2;
		} catch (NullPointerException e) {
			sentValue1 = 0.0;
			compareZeroNonZero.put(word1, 1.0);
		}
		try {
			sentValue2 = (1 - compareZeroNonZero.get(word2)) / 2;
		} catch (NullPointerException e) {
			sentValue2 = 0.0;
			compareZeroNonZero.put(word2, 1.0);
		}
		try {
			sentValue3 = (1 - compareZeroNonZero.get(word3)) / 2;
		} catch (NullPointerException e) {
			sentValue3 = 0.0;
			compareZeroNonZero.put(word3, 1.0);
		}
		if (sentValue1 != 0)
			sentValue1 = sentValue1 * comparePosNeg.get(word1);
		if (sentValue2 != 0)
			sentValue2 = sentValue2 * comparePosNeg.get(word2);
		if (sentValue3 != 0)
			sentValue3 = sentValue3 * comparePosNeg.get(word3);
		return (sentValue1 + sentValue2 + sentValue3) / 3;
	}

	public static double[] kMeansClustering(Map<String, Double> map,
			double upper, double lower) {
		// FileWriter outFile = new FileWriter("TweetWithValues.txt");
		// PrintWriter out = new PrintWriter(outFile);
		double centroidLower = lower;
		double centroidLowerOld = lower;
		double centroidUpperOld = upper;
		double centroidUpper = upper;
		double centroidNeutraal = 0.0f;
		double centroidNeutraalOld = 0.0f;
		int aantalLower = 0, aantalNeutraal = 0;
		int aantalUpper = 0;
		double sentiment;
		Boolean convergence = false;
		double lowerdiff;
		double higherdiff;
		double neutraldiff;
		double totalUpper = upper, totalNeutraal = 0.0f, extremeNeutraalLow = 0.0f, extremeNeutraalUp = 0.0f;
		double extremeLower = -10;
		double extremeUpper = 0;
		double totalLower = lower;
		
		// Map<String, Double> woordenweightslijst =
		// StandardFunctions.getWeightsList();;
		while (!convergence) {
			// Iterator<String> k = (Iterator<String>) map.keySet().iterator();
			List<String> list = new ArrayList<String>(map.keySet());
			Iterator<String> k = list.iterator();
			extremeNeutraalLow = 0;
			extremeNeutraalUp = 0;
			extremeLower = -10;
			extremeUpper = 10;

			centroidUpperOld = centroidUpper;

			centroidLowerOld = centroidLower;
			centroidNeutraalOld = centroidNeutraal;
			while (k.hasNext()) {

				String word = k.next();

				sentiment = map.get(word);
				// System.out.println(word);
				lowerdiff = Math.abs(sentiment - centroidLower);
				higherdiff = Math.abs(sentiment - centroidUpper);
				neutraldiff = Math.abs(sentiment - centroidNeutraal);
				if ((lowerdiff < higherdiff) && (lowerdiff < neutraldiff)) {
					aantalLower++;
					totalLower = totalLower + sentiment;
					if (sentiment > extremeLower) {
						extremeLower = sentiment;
					}
				} else if ((higherdiff < lowerdiff)
						&& (higherdiff < neutraldiff)) {
					aantalUpper++;
					totalUpper += sentiment;
					if (sentiment < extremeUpper) {
						extremeUpper = sentiment;
					}
				} else {
					aantalNeutraal++;
					totalNeutraal += sentiment;
					if (sentiment < extremeNeutraalLow) {
						extremeNeutraalLow = sentiment;
					} else if (sentiment > extremeNeutraalUp) {
						extremeNeutraalUp = sentiment;
					}
				}
			}
			centroidLower = totalLower / aantalLower;
			centroidUpper = totalUpper / aantalUpper;
			centroidNeutraal = totalNeutraal / aantalNeutraal;
			
			System.out.println(" centroidUpper = " + centroidUpper
					+ " aantalUpper = " + aantalUpper + " centroidLower = "
					+ centroidLower + " aantalLower = " + aantalLower
					+ " centroidNeutraal = " + centroidNeutraal
					+ " aantalNeutraal = " + aantalNeutraal);
			System.out.println("ExtremeUp = " + extremeUpper + " ExtremeLow = "
					+ extremeLower + " ExtremeNeutraalUp = "
					+ extremeNeutraalUp + " extremeNeutraalLow = "
					+ extremeNeutraalLow);
			totalLower = 0;
			totalUpper = 0;
			aantalNeutraal = 0;
			totalNeutraal = 0;
			aantalLower = 0;
			aantalUpper = 0;

			if ((centroidUpper == centroidUpperOld)
					&& (centroidLower == centroidLowerOld)
					&& (centroidNeutraal == centroidNeutraalOld)) {
				convergence = true;
			}

		}
		return (new double[] { (extremeUpper + extremeNeutraalUp) / 2,
				(extremeLower + extremeNeutraalLow) / 2, centroidNeutraal }); // centroids
																				// gebruiken
																				// dan?
																				// Ik
																				// dacht
																				// de
																				// meest
																				// extreme
																				// waarde
																				// per
																				// cluster

	}

}
